﻿package com.zhiduan.crowdclient.util;

import com.zhiduan.crowdclient.MyApplication;

import android.content.Context;
import android.content.SharedPreferences;

public class SharedPreferencesUtils {
	
	private static final String FILE_NAME = "share_date";

	public SharedPreferencesUtils(){
		
	}
	/**
	 * 保存数据的方法，我们需要拿到保存数据的具体类型，然后根据类型调用不同的保存方法
	 * 
	 * @param context
	 * @param key  保存对应的KEY
	 * @param object  保存对应的值
	 */
	public static void setParam(String key, Object object) {

		Context context = MyApplication.getInstance();
		
		String type = object.getClass().getSimpleName();
		SharedPreferences sp = context.getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE);
		SharedPreferences.Editor editor = sp.edit();

		if ("String".equals(type)) {
			editor.putString(key, (String) object);
		} else if ("Integer".equals(type)) {
			editor.putInt(key, (Integer) object);
		} else if ("Boolean".equals(type)) {
			editor.putBoolean(key, (Boolean) object);
		} else if ("Float".equals(type)) {
			editor.putFloat(key, (Float) object);
		} else if ("Long".equals(type)) {
			editor.putLong(key, (Long) object);
		}

		editor.commit();
	}

	/**
	 * 得到保存数据的方法，我们根据默认值得到保存的数据的具体类型，然后调用相对于的方法获取值
	 * 
	 * @param context
	 * @param key
	 * @param defaultObject
	 * @return
	 */
	public static Object getParam(String key, Object defaultObject) {
		
		try {
			Context context = MyApplication.getInstance();
			
			String type = defaultObject.getClass().getSimpleName();
			SharedPreferences sp = context.getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE);
			Logs.v("zd", type + "/" + key + "/" + defaultObject.toString());
			if ("String".equals(type)) {
				return sp.getString(key, (String) defaultObject);
			} else if ("Integer".equals(type)) {
				return sp.getInt(key, (Integer) defaultObject);
			} else if ("Boolean".equals(type)) {
				return sp.getBoolean(key, (Boolean) defaultObject);
			} else if ("Float".equals(type)) {
				return sp.getFloat(key, (Float) defaultObject);
			} else if ("Long".equals(type)) {
				return sp.getLong(key, (Long) defaultObject);
			}

			return null;
		} catch (Exception e) {
			// TODO: handle exception
			return null;
		}
		

		
	}

}
